#include "mainscene.h"
#include "ui_mainscene.h"
#include "gameconfig.h"
#include "gamecontroller.h"
#include "gamepushbutton.h"
#include <QPixmap>
#include <QPainter>
#include <QDebug>
#include <QTimer>
MainScene::MainScene(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainScene)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("翻转金币游戏"));
    this->setFixedSize(GAME_WIDTH, GAME_HEIGHT); // 固定窗口禁止拖拉缩放
    this->setWindowIcon(QIcon(IMG_GAME_ICON));

    //
    GameController::getInstance()->initMainScene(this);

    //
    GamePushButton *btnStart = new GamePushButton(BTN_START_GAME, "");
    btnStart->setParent(this);
    btnStart->move((GAME_WIDTH - btnStart->width()) / 2, GAME_HEIGHT - 150);
    connect(btnStart, &QPushButton::clicked, this, [=]()
    {
       qDebug() << "clicked btnStrart";
       GameController::getInstance()->playStartSound();
       btnStart->zoomUp();
       btnStart->zoomDown();

       QTimer::singleShot(300, this, [=]()
       {
           // 进入关卡界面
           GameController::getInstance()->playFlipCoinSound();
           GameController::getInstance()->hideMainScene();
           GameController::getInstance()->showLevelScene();
       });
    });
}

MainScene::~MainScene()
{
    delete ui;
}


void MainScene::on_action_quit_triggered()
{
    //exit(0);
    GameController::getInstance()->hideMainScene();
    GameController::getInstance()->showLevelScene();
}

void MainScene::paintEvent(QPaintEvent *event)
{
    QPainter mainPainter(this);
    QPixmap mainPixmap;
    bool ret = mainPixmap.load(IMG_BG_MAINSCENE);
    if(ret)
    {
        mainPainter.drawPixmap(0, 0, mainPixmap);
    }
}

